<?php

  include "DALproduct.php";

  //retrieves information about the product
  function validateProductDetails($description, $dep, $brand, $size, $colour, $price1, $price2)
  {
    if($description == "" || empty($description) || $dep == "0")
    {
	  echo "<SCRIPT language='Javascript'>";
      echo "  alert('You must enter all mandatory fields.');";
      echo "</SCRIPT>";
      return false;
    }
	else
    {
      if($description != "" || !empty($description))
      {
        if(strlen($description) > 150)
        {
          echo "<SCRIPT language='Javascript'>";
          echo "  alert('The brand must be 150 characters or less.');";
          echo "</SCRIPT>";
          return false;
        }
      }
      if($brand != "" || !empty($brand))
      {
        if(strlen($brand) > 50)
        {
          echo "<SCRIPT language='Javascript'>";
          echo "  alert('The brand must be 50 characters or less.');";
          echo "</SCRIPT>";
          return false;
        }
      }
      if($size != "" || !empty($size))
      {
        if(strlen($size) > 5)
        {
          echo "<SCRIPT language='Javascript'>";
          echo "  alert('The size must be 5 characters or less.');";
          echo "</SCRIPT>";
          return false;
        }
      }
      if($colour != "" || !empty($colour))
      {
        if(strlen($colour) > 30)
        {
          echo "<SCRIPT language='Javascript'>";
          echo "  alert('The size must be 30 characters or less.');";
          echo "</SCRIPT>";
          return false;
        }
      }
      if($price1 != "" || !empty($price1) || $price2 != "" || !empty($price2))
      {
        if(strlen($price1) > 6 || 0 >= strlen($price1) || strlen($price2) != 2)
        {
          echo "<SCRIPT language='Javascript'>";
          echo "  alert('The price must have 1-6 digits on the left and 2 digits on the right of the decimal point.');";
          echo "</SCRIPT>";
          return false;
        }
		else
        {
          if(isNumber($price1) == false || isNumber($price2) == false)
          {
            return false;
          }
        }
      }
      return true;
    }
  }

  function getSearchedDetails($description, $dep, $brand, $size, $colour, $price1, $price2)
  {
    return getProductDetails($description, findDepNo($dep), $brand, $size, $colour, $price1, $price2);
  }

  function getRelatedDetails($description, $dep, $brand, $size, $colour, $price1, $price2)
  {
    return getRelatedProductDetails($description, findDepNo($dep), $brand, $size, $colour, $price1, $price2);
  }

  function findDepNo($dep)
  {
    return getDepNo($dep);
  }

  function findStockLevel($prod_no)
  {
    return getStockLevel($prod_no);
  }

  function findDepName($prod_no)
  {
    return getDepName($prod_no);
  }

  function findAllDepartments()
  {
    $dep = getDepartments();
    if($dep == "0")
    {
      echo "<SCRIPT language='Javascript'>\n";
      echo "  alert('There appears to be no departments available.  Please come back later.');";
      echo "  window.location.href = 'main.php';";
      echo "</SCRIPT>";
    }
    else
    {
      return $dep;
    }
  }

  function findProductDetails()
  {
    return getAllProductDetails();
  }

  function findStockLevels()
  {
    return getStockLevels();
  }

  function isNumber($number)
  {
    for($i = 0; $i < strlen($number); $i++)
    {
	  $n = $number[$i];
      if($n == '0' || $n == '1' || $n == '2' || $n == '3' || $n == '4' || $n == '5' || $n == '6' || $n == '7' || $n == '8' || $n == '9')
	  {
	  }
	  else
      {
        return false;
      }
    }
    return true;
  }

?>